﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class CashRegisterController : Controller
    {

        public ActionResult CashRegisterIndex()
        {
            return View();
        }

        public ActionResult GetList(PageVM pvm)
        {
            CashRegisterInfo info = new CashRegisterInfo();
            string condition = pvm.query;
            info.year = condition.Split(',')[0].ToString();
            info.month = condition.Split(',')[1].ToString();

            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            string tablename = "TMP_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            int count;
            DataSet list = info.BindList(pvm.rp, pvm.page, info, tablename, out count);
            jsonFlexiGridData.total = count;
            List<string> v;
            int count2120add = 0;
            decimal sum2120add = 0.0M;
            int count2237add = 0;
            decimal sum2237add = 0.0M;
            int count25add = 0;
            decimal sum25add = 0.0M;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                count2120add += Convert.ToInt32(item["COUNTM21"]);
                sum2120add += Convert.ToDecimal(item["SUMM21"]);
                count2237add += (Convert.ToInt32(item["COUNTM22"]));
                sum2237add += (Convert.ToDecimal(item["SUMM22"]));
                count25add += Convert.ToInt32(item["COUNTM38"]);
                sum25add += Convert.ToDecimal(item["SUMM38"]);
            }
            v = new List<string>();
            v.Add("1");
            v.Add(count2120add.ToString());
            v.Add(sum2120add.ToString());
            v.Add(count2237add.ToString());
            v.Add(sum2237add.ToString());
            v.Add(count25add.ToString());
            v.Add(sum25add.ToString());
            v.Add(sum2120add.ToString());
            v.Add(sum2237add.ToString());
            FlexiGridRow fgr = new FlexiGridRow()
            {
                id = "1",
                cell = v
            };
            jsonFlexiGridData.rows.Add(fgr);
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 现金出纳表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string year, string month)
        {
            return EContent(year, month);
        }

        public ExcelContentResult EContent(string year, string month)
        {
            CashRegisterInfo info = new CashRegisterInfo();
            info.year = year;
            info.month = month;
            string tablename = "TMP_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            StringBuilder sBuilder = new StringBuilder();
            DataSet list = info.GetListAll(info, tablename);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='6' style='font-size:18px; text-align:center;'>上海港集装箱公司外高桥码头公司食堂餐饮管理信息系统</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='6' style='font-size:25px; text-align:center;font-weight:bold'>现金出纳表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            string yy = year;
            string mm = month;
            string yy1 = yy;
            string mm1 = (Convert.ToInt32(mm) - 1).ToString();
            if (Convert.ToInt32(mm) == 1)
            {
                yy1 = (Convert.ToInt32(yy) - 1).ToString();
                mm1 = "12";
            }
            string begin = yy1 + "/" + mm1 + "/" + "21";
            string end = yy + "/" + mm + "/" + "20";
            int count2120add = 0;
            decimal sum2120add = 0.0M;
            int count2237add = 0;
            decimal sum2237add = 0.0M;
            int count25add = 0;
            decimal sum25add = 0.0M;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                count2120add += Convert.ToInt32(item["COUNTM21"]);
                sum2120add += Convert.ToDecimal(item["SUMM21"]);
                count2237add += (Convert.ToInt32(item["COUNTM22"]));
                sum2237add += (Convert.ToDecimal(item["SUMM22"]));
                count25add += Convert.ToInt32(item["COUNTM38"]);
                sum25add += Convert.ToDecimal(item["SUMM38"]);
            }
            sBuilder.Append("<td colspan='6' style='font-size:14px;text-align:left;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;统计日期：" +
                begin + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;到" + end + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;统计范围：全部</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px;'>");
            sBuilder.Append("<td>交费人数</td>");
            sBuilder.Append("<td>" + count2120add.ToString() + "</td>");
            sBuilder.Append("<td>补贴人数</td>");
            sBuilder.Append("<td>" + count2237add.ToString() + "</td>");
            sBuilder.Append("<td>退款人数</td>");
            sBuilder.Append("<td>" + count25add.ToString() + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px;'>");
            sBuilder.Append("<td>交费金额</td>");
            sBuilder.Append("<td>" + sum2120add.ToString() + "</td>");
            sBuilder.Append("<td>补贴金额</td>");
            sBuilder.Append("<td>" + sum2237add.ToString() + "</td>");
            sBuilder.Append("<td>退款金额</td>");
            sBuilder.Append("<td>" + sum25add.ToString() + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px;'>");
            sBuilder.Append("<td>实收现金</td>");
            sBuilder.Append("<td>" + sum2120add.ToString().Trim() + "</td>");
            sBuilder.Append("<td colspan='4'>(大写)" + ConvertSum(sum2120add.ToString()) + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px;'>");
            sBuilder.Append("<td>实收支票</td>");
            sBuilder.Append("<td>" + sum2237add.ToString().Trim() + "</td>");
            sBuilder.Append("<td colspan='4'>(大写)" + ConvertSum(sum2237add.ToString()) + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            sBuilder.Append("<\br>");
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='0'>");
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;打印日期：" + DateTime.Now.ToString("yyyy/MM/dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "打印时间：" + DateTime.Now.ToString("HH:mm:ss") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;制表人：" + ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;收款员签字：</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "zengjianhuisong";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }

        //作者：fanz2000
        //Email:fanz2000@sohu.com
        /// 
        /// 转换数字金额主函数（包括小数）
        /// 
        /// 数字字符串
        /// 转换成中文大写后的字符串或者出错信息提示字符串
        public string ConvertSum(string str)
        {
            if (!IsPositveDecimal(str))
                return "输入的不是正数字！";
            if (Double.Parse(str) > 999999999999.99)
                return "数字太大，无法换算，请输入一万亿元以下的金额";
            char[] ch = new char[1];
            ch[0] = '.'; //小数点
            string[] splitstr = null; //定义按小数点分割后的字符串数组
            splitstr = str.Split(ch[0]);//按小数点分割字符串
            if (splitstr.Length == 1) //只有整数部分
                return ConvertData(str) + "元整";
            else //有小数部分
            {
                string rstr;
                rstr = ConvertData(splitstr[0]) + "元";//转换整数部分
                rstr += ConvertXiaoShu(splitstr[1]);//转换小数部分
                return rstr;
            }

        }
        /// 
        /// 判断是否是正数字字符串
        /// 
        /// 判断字符串
        /// 如果是数字，返回true，否则返回false
        public bool IsPositveDecimal(string str)
        {
            Decimal d;
            try
            {
                d = Decimal.Parse(str);

            }
            catch (Exception)
            {
                return false;
            }
            if (d > 0)
                return true;
            else
                return false;
        }
        /// 
        /// 转换数字（整数）
        /// 
        /// 需要转换的整数数字字符串
        /// 转换成中文大写后的字符串
        public string ConvertData(string str)
        {
            string tmpstr = "";
            string rstr = "";
            int strlen = str.Length;
            if (strlen <= 4)//数字长度小于四位
            {
                rstr = ConvertDigit(str);

            }
            else
            {

                if (strlen <= 8)//数字长度大于四位，小于八位
                {
                    tmpstr = str.Substring(strlen - 4, 4);//先截取最后四位数字
                    rstr = ConvertDigit(tmpstr);//转换最后四位数字
                    tmpstr = str.Substring(0, strlen - 4);//截取其余数字
                    //将两次转换的数字加上萬后相连接
                    rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr);
                    rstr = rstr.Replace("零万", "万");
                    rstr = rstr.Replace("零零", "万");

                }
                else
                    if (strlen <= 12)//数字长度大于八位，小于十二位
                    {
                        tmpstr = str.Substring(strlen - 4, 4);//先截取最后四位数字
                        rstr = ConvertDigit(tmpstr);//转换最后四位数字
                        tmpstr = str.Substring(strlen - 8, 4);//再截取四位数字
                        rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr);
                        tmpstr = str.Substring(0, strlen - 8);
                        rstr = String.Concat(ConvertDigit(tmpstr) + "億", rstr);
                        rstr = rstr.Replace("零億", "億");
                        rstr = rstr.Replace("零萬", "零");
                        rstr = rstr.Replace("零零", "零");
                        rstr = rstr.Replace("零零", "零");
                    }
            }
            strlen = rstr.Length;
            if (strlen >= 2)
            {
                switch (rstr.Substring(strlen - 2, 2))
                {
                    case "佰零": rstr = rstr.Substring(0, strlen - 2) + "佰"; break;
                    case "仟零": rstr = rstr.Substring(0, strlen - 2) + "仟"; break;
                    case "万零": rstr = rstr.Substring(0, strlen - 2) + "万"; break;
                    case "億零": rstr = rstr.Substring(0, strlen - 2) + "億"; break;

                }
            }

            return rstr;
        }
        /// 
        /// 转换数字（小数部分）
        /// 
        /// 需要转换的小数部分数字字符串
        /// 转换成中文大写后的字符串
        public string ConvertXiaoShu(string str)
        {
            int strlen = str.Length;
            string rstr;
            if (strlen == 1)
            {
                rstr = ConvertChinese(str) + "角";
                return rstr;
            }
            else
            {
                string tmpstr = str.Substring(0, 1);
                rstr = ConvertChinese(tmpstr) + "角";
                tmpstr = str.Substring(1, 1);
                rstr += ConvertChinese(tmpstr) + "分";
                rstr = rstr.Replace("零分", "");
                rstr = rstr.Replace("零角", "");
                return rstr;
            }


        }

        /// 
        /// 转换数字
        /// 
        /// 转换的字符串（四位以内）
        /// 
        public string ConvertDigit(string str)
        {
            int strlen = str.Length;
            string rstr = "";
            switch (strlen)
            {
                case 1: rstr = ConvertChinese(str); break;
                case 2: rstr = Convert2Digit(str); break;
                case 3: rstr = Convert3Digit(str); break;
                case 4: rstr = Convert4Digit(str); break;
            }
            rstr = rstr.Replace("拾零", "拾");
            strlen = rstr.Length;

            return rstr;
        }


        /// 
        /// 转换四位数字
        /// 
        public string Convert4Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string str3 = str.Substring(2, 1);
            string str4 = str.Substring(3, 1);
            string rstring = "";
            rstring += ConvertChinese(str1) + "仟";
            rstring += ConvertChinese(str2) + "佰";
            rstring += ConvertChinese(str3) + "拾";
            rstring += ConvertChinese(str4);
            rstring = rstring.Replace("零仟", "零");
            rstring = rstring.Replace("零佰", "零");
            rstring = rstring.Replace("零拾", "零");
            rstring = rstring.Replace("零零", "零");
            rstring = rstring.Replace("零零", "零");
            rstring = rstring.Replace("零零", "零");
            return rstring;
        }
        /// 
        /// 转换三位数字
        /// 
        public string Convert3Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string str3 = str.Substring(2, 1);
            string rstring = "";
            rstring += ConvertChinese(str1) + "佰";
            rstring += ConvertChinese(str2) + "拾";
            rstring += ConvertChinese(str3);
            rstring = rstring.Replace("零佰", "零");
            rstring = rstring.Replace("零拾", "零");
            rstring = rstring.Replace("零零", "零");
            rstring = rstring.Replace("零零", "零");
            return rstring;
        }
        /// 
        /// 转换二位数字
        /// 
        public string Convert2Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string rstring = "";
            rstring += ConvertChinese(str1) + "拾";
            rstring += ConvertChinese(str2);
            rstring = rstring.Replace("零拾", "零");
            rstring = rstring.Replace("零零", "零");
            return rstring;
        }
        /// 
        /// 将一位数字转换成中文大写数字
        /// 
        public string ConvertChinese(string str)
        {
            //"零壹贰叁肆伍陆柒捌玖拾佰仟萬億圆整角分"
            string cstr = "";
            switch (str)
            {
                case "0": cstr = "零"; break;
                case "1": cstr = "壹"; break;
                case "2": cstr = "贰"; break;
                case "3": cstr = "叁"; break;
                case "4": cstr = "肆"; break;
                case "5": cstr = "伍"; break;
                case "6": cstr = "陆"; break;
                case "7": cstr = "柒"; break;
                case "8": cstr = "捌"; break;
                case "9": cstr = "玖"; break;
            }
            return (cstr);
        }

    }
}
